home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / modes / f90.el.z / f90.el
Encoding:
Text File  |  1998-05-21  |  68.4 KB  |  1,696 lines

  1. ;;; f90.el --- Fortran-90 mode (free format)
  2.  
  3. ;; Copyright (C) 1995, 1996 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Torbj\"orn Einarsson <T.Einarsson@clab.ericsson.se>
  6. ;; Created: Apr. 18, 1996
  7. ;; Keywords: fortran, f90, languages
  8.  
  9. ;; This file is part of XEmacs.
  10.  
  11. ;; XEmacs is free software; you can redistribute it and/or modify it
  12. ;; under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2 of the License, or
  14. ;; (at your option) any later version.
  15.  
  16. ;; XEmacs is distributed in the hope that it will be useful, but
  17. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19. ;; General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with XEmacs; if not, write to the Free Software Foundation,
  23. ;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24.  
  25. ;;; Synched up with: FSF 19.34.
  26.  
  27. ;;; Commentary:
  28.  
  29. ;; Smart mode for editing F90 programs in FREE FORMAT.
  30. ;; Knows about continuation lines, named structured statements, and other
  31. ;; new features in F90 including HPF (High Performance Fortran) structures.
  32. ;; The basic feature is to provide an accurate indentation of F90 programs.
  33. ;; In addition, there are many more features like automatic matching of all
  34. ;; end statements, an auto-fill function to break long lines, a join-lines
  35. ;; function which joins continued lines etc etc.
  36. ;;  To facilitate typing, a fairly complete list of abbreviations is provided.
  37. ;;    For example, `i is short-hand for integer (if abbrev-mode is on).
  38.  
  39. ;; There are two separate features for highlighting the code.
  40. ;;   1) Upcasing or capitalizing of all keywords.
  41. ;;   2) Colors/fonts using font-lock-mode. (only when using X-windows)
  42. ;;  Automatic upcase of downcase of keywords is controlled by the parameter
  43. ;;  f90-auto-keyword-case.
  44.  
  45. ;; The indentations of lines starting with ! is determined by the first of the
  46. ;; following matches (the values in the left column are the default values):
  47.  
  48. ;; start-string/regexp  indent         variable holding start-string/regexp
  49. ;;    !!!                  0
  50. ;;    !hpf\\$ (re)         0              f90-directive-comment-re
  51. ;;    !!$                  0              f90-comment-region
  52. ;;    !      (re)        as code          f90-indented-comment-re
  53. ;;    default            comment-column
  54.  
  55. ;; Ex: Here is the result of 3 different settings of f90-indented-comment-re
  56. ;;     f90-indented-comment-re  !-indentation      !!-indentation
  57. ;;          !                    as code             as code
  58. ;;          !!                   comment-column      as code
  59. ;;          ![^!]                as code             comment-column
  60. ;; Trailing comments are indented to comment-column with indent-for-comment M-;
  61. ;; f90-comment-region (C-c;) toggles insertion of f90-comment-region in region.
  62.  
  63. ;; One common convention for free vs. fixed format is that free-format files
  64. ;; have the ending .f90 while the fixed format files have the ending .f. 
  65. ;; To make f90-mode work, put this file in, for example, your directory
  66. ;;  ~/lisp, and be sure that you have the following in your .emacs-file
  67. ;;     (setq load-path (append load-path '("~/lisp")))
  68. ;;     (autoload 'f90-mode "f90"
  69. ;;       "Major mode for editing Fortran 90 code in free format." t)
  70. ;;     (setq auto-mode-alist (append auto-mode-alist 
  71. ;;                           (list '("\\.f90$" . f90-mode))))
  72. ;; Once you have entered f90-mode, you may get more info by using
  73. ;; the command describe-mode (C-h m). For online help describing various
  74. ;; functions use  C-h f <Name of function you want described>
  75.  
  76. ;; To customize the f90-mode for your taste, use, for example:
  77. ;;    (you don't have to specify values for all the parameters below)
  78. ;;(setq f90-mode-hook
  79. ;;      '(lambda () (setq f90-do-indent 3
  80. ;;                        f90-if-indent 3
  81. ;;                        f90-type-indent 3
  82. ;;                        f90-program-indent 2
  83. ;;                        f90-continuation-indent 5
  84. ;;                        f90-comment-region "!!$"
  85. ;;                        f90-directive-comment-re "!hpf\\$"
  86. ;;                        f90-indented-comment-re "!"
  87. ;;                        f90-break-delimiters "[-+\\*/,><=% \t]"
  88. ;;                        f90-break-before-delimiters t
  89. ;;                        f90-beginning-ampersand t
  90. ;;                        f90-smart-end 'blink
  91. ;;                        f90-auto-keyword-case nil
  92. ;;                        f90-leave-line-no  nil
  93. ;;                        f90-startup-message t
  94. ;;                        indent-tabs-mode nil
  95. ;;                  )
  96. ;;       ;;The rest is not default.
  97. ;;       (abbrev-mode 1)             ; turn on abbreviation mode
  98. ;;       (f90-auto-fill-mode 1)      ; turn on auto-filling
  99. ;;       (turn-on-font-lock)         ; for highlighting
  100. ;;       (if f90-auto-keyword-case   ; change case of all keywords on startup
  101. ;;           (f90-change-keywords f90-auto-keyword-case))
  102. ;;     ))
  103. ;; in your .emacs file (the shown values are the defaults). You can also
  104. ;; change the values of the lists f90-keywords etc.
  105. ;; The auto-fill and abbreviation minor modes are accessible from the menu,
  106. ;; or by using M-x f90-auto-fill-mode and M-x abbrev-mode, respectively.
  107.  
  108. ;; Remarks
  109. ;; 1) Line numbers are by default left-justified. If f90-leave-line-no is
  110. ;;    non-nil, the line numbers are never touched.
  111. ;; 2) Multi-; statements like > do i=1,20 ; j=j+i ; end do < are not handled
  112. ;;    correctly, but I imagine them to be rare.
  113. ;; 3) Regexps for hilit19 are no longer supported.
  114. ;; 4) For FIXED FORMAT code, use the ordinary fortran mode.
  115. ;; 5) This mode does not work under emacs-18.x.
  116. ;; 6) Preprocessor directives, i.e., lines starting with # are left-justified
  117. ;;    and are untouched by all case-changing commands. There is, at present, no
  118. ;;    mechanism for treating multi-line directives (continued by \ ).
  119. ;; 7) f77 do-loops do 10 i=.. ; ; 10 continue are not correctly indented.
  120. ;;    You are urged to use f90-do loops (with labels if you wish).
  121. ;; 8) The highlighting mode under XEmacs is not as complete as under Emacs.
  122.  
  123. ;; List of user commands
  124. ;;   f90-previous-statement         f90-next-statement
  125. ;;   f90-beginning-of-subprogram    f90-end-of-subprogram   f90-mark-subprogram
  126. ;;   f90-comment-region
  127. ;;   f90-indent-line                f90-indent-new-line
  128. ;;   f90-indent-region    (can be called by calling indent-region)
  129. ;;   f90-indent-subprogram
  130. ;;   f90-break-line                 f90-join-lines
  131. ;;   f90-auto-fill-mode
  132. ;;   f90-fill-region
  133. ;;   f90-insert-end
  134. ;;   f90-upcase-keywords            f90-upcase-region-keywords
  135. ;;   f90-downcase-keywords          f90-downcase-region-keywords
  136. ;;   f90-capitalize-keywords        f90-capitalize-region-keywords
  137.  
  138. ;; Thanks to all the people who have tested the mode. Special thanks to Jens
  139. ;; Bloch Helmers for encouraging me to write this code, for creative
  140. ;; suggestions as well as for the lists of hpf-commands.
  141. ;; Also thanks to the authors of the fortran and pascal modes, on which some
  142. ;; of this code is built.
  143.  
  144. ;;; Code:
  145.  
  146. (eval-when-compile
  147.   (defvar deactivate-mark)
  148.   (defvar mark-active))
  149.  
  150. (defconst bug-f90-mode "T.Einarsson@clab.ericsson.se"
  151.   "Address of mailing list for F90 mode bugs.")
  152.  
  153. ;; User options
  154.  
  155. (defgroup f90 nil
  156.   "Fortran-90 mode"
  157.   :group 'fortran)
  158.  
  159. (defgroup f90-indent nil
  160.   "Fortran-90 indentation"
  161.   :prefix "f90-"
  162.   :group 'f90)
  163.  
  164.  
  165. (defcustom f90-do-indent 3
  166.   "*Extra indentation applied to DO blocks."
  167.   :type 'integer
  168.   :group 'f90-indent)
  169.  
  170. (defcustom f90-if-indent 3
  171.   "*Extra indentation applied to IF, SELECT CASE, WHERE and FORALL blocks."
  172.   :type 'integer
  173.   :group 'f90-indent)
  174.  
  175. (defcustom f90-type-indent 3
  176.   "*Extra indentation applied to TYPE, INTERFACE and BLOCK DATA blocks."
  177.   :type 'integer
  178.   :group 'f90-indent)
  179.  
  180. (defcustom f90-program-indent 2
  181.   "*Extra indentation applied to PROGRAM/MODULE/SUBROUTINE/FUNCTION blocks."
  182.   :type 'integer
  183.   :group 'f90-indent)
  184.  
  185. (defcustom f90-continuation-indent 5
  186.   "*Extra indentation applied to F90 continuation lines."
  187.   :type 'integer
  188.   :group 'f90-indent)
  189.  
  190. (defcustom f90-comment-region "!!$"
  191.   "*String inserted by \\[f90-comment-region]\
  192.  at start of each line in region."
  193.   :type 'string
  194.   :group 'f90-indent)
  195.  
  196. (defcustom f90-indented-comment-re "!"
  197.   "*Regexp saying which comments to be indented like code."
  198.   :type 'regexp
  199.   :group 'f90-indent)
  200.  
  201. (defcustom f90-directive-comment-re "!hpf\\$"
  202.   "*Regexp of comment-like directive like \"!HPF\\\\$\", not to be indented."
  203.   :type 'regexp
  204.   :group 'f90-indent)
  205.  
  206. (defcustom f90-beginning-ampersand t
  207.   "*t makes automatic insertion of \& at beginning of continuation line."
  208.   :type 'boolean
  209.   :group 'f90)
  210.  
  211. (defcustom f90-smart-end 'blink
  212.   "*From an END statement, check and fill the end using matching block start.
  213. Allowed values are 'blink, 'no-blink, and nil, which determine
  214. whether to blink the matching beginning."
  215.   :type '(choice (const blink) (const no-blink) (const nil))
  216.   :group 'f90)
  217.  
  218. (defcustom f90-break-delimiters "[-+\\*/><=,% \t]"
  219.   "*Regexp holding list of delimiters at which lines may be broken."
  220.   :type 'regexp
  221.   :group 'f90)
  222.  
  223. (defcustom f90-break-before-delimiters t
  224.   "*Non-nil causes `f90-do-auto-fill' to break lines before delimiters."
  225.   :type 'regexp
  226.   :group 'f90)
  227.  
  228. (defcustom f90-auto-keyword-case nil
  229.   "*Automatic case conversion of keywords.
  230.   The options are 'downcase-word, 'upcase-word, 'capitalize-word and nil"
  231.   :type '(choice (const downcase-word) (const upcase-word)
  232.          (const capitalize-word) (const nil))
  233.   :group 'f90)
  234.  
  235. (defcustom f90-leave-line-no nil
  236.   "*If nil, left-justify linenumbers."
  237.   :type 'boolean
  238.   :group 'f90)
  239.  
  240. (defcustom f90-startup-message t
  241.   "*Non-nil displays a startup message when F90 mode is first called."
  242.   :type 'boolean
  243.   :group 'f90)
  244.  
  245. (defconst f90-keywords-re
  246.   ;;("allocate" "allocatable" "assign" "assignment" "backspace" "block"
  247.   ;;"call" "case" "character" "close" "common" "complex" "contains"
  248.   ;;"continue" "cycle" "data" "deallocate" "dimension" "do" "double" "else"
  249.   ;;"elseif" "elsewhere" "end" "enddo" "endfile" "endif" "entry" "equivalence"
  250.   ;;"exit" "external" "forall" "format" "function" "goto" "if" "implicit"
  251.   ;;"include" "inquire" "integer" "intent" "interface" "intrinsic" "logical"
  252.   ;;"module" "namelist" "none" "nullify" "only" "open" "operator" "optional" "parameter"
  253.   ;;"pause" "pointer" "precision" "print" "private" "procedure" "program"
  254.   ;;"public" "read" "real" "recursive" "result" "return" "rewind" "save" "select"
  255.   ;;"sequence" "stop" "subroutine" "target" "then" "type" "use" "where"
  256.   ;;"while" "write")
  257.   (concat
  258.    "\\<\\(a\\(llocat\\(able\\|e\\)\\|ssign\\(\\|ment\\)\\)\\|b\\(ackspace\\|"
  259.    "lock\\)\\|c\\(a\\(ll\\|se\\)\\|haracter\\|lose\\|o\\(m\\(mon\\|plex\\)\\|"
  260.    "nt\\(ains\\|inue\\)\\)\\|ycle\\)\\|d\\(ata\\|eallocate\\|imension\\|"
  261.    "o\\(\\|uble\\)\\)\\|e\\(lse\\(\\|if\\|where\\)\\|n\\(d\\(\\|do\\|file\\|"
  262.    "if\\)\\|try\\)\\|quivalence\\|x\\(it\\|ternal\\)\\)\\|f\\(or\\(all\\|"
  263.    "mat\\)\\|unction\\)\\|goto\\|i\\(f\\|mplicit\\|n\\(clude\\|quire\\|t\\("
  264.    "e\\(ger\\|nt\\|rface\\)\\|rinsic\\)\\)\\)\\|logical\\|module\\|n\\("
  265.    "amelist\\|one\\|ullify\\)\\|o\\(nly\\|p\\(en\\|erator\\|tional\\)\\)\\|p\\(a\\("
  266.    "rameter\\|use\\)\\|ointer\\|r\\(ecision\\|i\\(nt\\|vate\\)\\|o\\("
  267.    "cedure\\|gram\\)\\)\\|ublic\\)\\|re\\(a[dl]\\|cursive\\|sult\\|turn\\|wind\\)\\|"
  268.    "s\\(ave\\|e\\(lect\\|quence\\)\\|top\\|ubroutine\\)\\|t\\(arget\\|hen\\|"
  269.    "ype\\)\\|use\\|w\\(h\\(ere\\|ile\\)\\|rite\\)\\)\\>")
  270.   "Regexp for F90 keywords.")
  271.  
  272. (defconst f90-keywords-level-3-re
  273.   ;; ("allocate" "allocatable" "assign" "assignment" "backspace" "close"
  274.   ;; "deallocate" "dimension" "endfile" "entry" "equivalence" "external"
  275.   ;; "inquire" "intent" "intrinsic" "nullify" "only" "open" "operator"
  276.   ;; "optional" "parameter" "pause" "pointer" "print" "private" "public"
  277.   ;; "read" "recursive" "result" "rewind" "save" "select" "sequence"
  278.   ;; "target"  "write")
  279.   (concat
  280.    "\\<\\(a\\(llocat\\(able\\|e\\)\\|ssign\\(\\|ment\\)\\)\\|backspace\\|"
  281.    "close\\|d\\(eallocate\\|imension\\)\\|e\\(n\\(dfile\\|try\\)\\|"
  282.    "quivalence\\|xternal\\)\\|"
  283.    "in\\(quire\\|t\\(ent\\|rinsic\\)\\)\\|nullify\\|"
  284.    "o\\(nly\\|p\\(en\\|erator\\|tional\\)\\)\\|"
  285.    "p\\(a\\(rameter\\|use\\)\\|ointer\\|ri\\(nt\\|vate\\)\\|ublic\\)\\|re\\("
  286.    "ad\\|cursive\\|sult\\|wind\\)\\|s\\(ave\\|e\\(lect\\|quence\\)\\)\\|target\\|"
  287.    "write\\)\\>")
  288. "Keyword-regexp for font-lock level >= 3.")
  289.  
  290.  
  291. (defconst f90-procedures-re
  292.   ;; ("abs" "achar" "acos" "adjustl" "adjustr" "aimag" "aint" "all" "allocated"
  293.   ;; "anint" "any" "asin" "associated" "atan" "atan2" "bit_size" "btest"
  294.   ;; "ceiling" "char" "cmplx" "conjg" "cos" "cosh" "count" "cshift"
  295.   ;; "date_and_time" "dble" "digits" "dim" "dot_product" "dprod" "eoshift"
  296.   ;; "epsilon" "exp" "exponent" "floor" "fraction" "huge" "iachar" "iand"
  297.   ;; "ibclr" "ibits" "ibset" "ichar" "ieor" "index" "int" "ior" "ishft"
  298.   ;; "ishftc" "kind" "lbound" "len" "len_trim" "lge" "lgt" "lle" "llt" "log"
  299.   ;; "logical" "log10" "matmul" "max" "maxexponent" "maxloc" "maxval" "merge"
  300.   ;; "min" "minexponent" "minloc" "minval" "mod" "modulo" "mvbits" "nearest"
  301.   ;; "nint" "not" "pack" "precision" "present" "product" "radix"
  302.   ;; "random_number" "random_seed" "range" "real" "repeat" "reshape"
  303.   ;; "rrspacing" "scale" "scan" "selected_int_kind" "selected_real_kind"
  304.   ;; "set_exponent" "shape" "sign" "sin" "sinh" "size" "spacing" "spread"
  305.   ;; "sqrt" "sum" "system_clock" "tan" "tanh" "tiny" "transfer" "transpose"
  306.   ;; "trim" "ubound" "unpack" "verify")
  307.   ;; A left paranthesis to avoid highlighting non-procedures.
  308.   ;; Real is taken out here to avoid highlighting declarations.
  309.   (concat
  310.    "\\<\\(a\\(bs\\|c\\(har\\|os\\)\\|djust[lr]\\|i\\(mag\\|nt\\)\\|ll\\(\\|"
  311.    "ocated\\)\\|n\\(int\\|y\\)\\|s\\(in\\|sociated\\)\\|tan2?\\)\\|b\\("
  312.    "it_size\\|test\\)\\|c\\(eiling\\|har\\|mplx\\|o\\(njg\\|sh?\\|unt\\)\\|"
  313.    "shift\\)\\|d\\(ate_and_time\\|ble\\|i\\(gits\\|m\\)\\|ot_product\\|prod"
  314.    "\\)\\|e\\(oshift\\|psilon\\|xp\\(\\|onent\\)\\)\\|f\\(loor\\|"
  315.    "raction\\)\\|huge\\|i\\(a\\(char\\|nd\\)\\|b\\(clr\\|its\\|set\\)\\|"
  316.    "char\\|eor\\|n\\(dex\\|t\\)\\|or\\|shftc?\\)\\|kind\\|l\\(bound\\|"
  317.    "en\\(\\|_trim\\)\\|g[et]\\|l[et]\\|og\\(\\|10\\|ical\\)\\)\\|m\\(a\\("
  318.    "tmul\\|x\\(\\|exponent\\|loc\\|val\\)\\)\\|erge\\|in\\(\\|exponent\\|"
  319.    "loc\\|val\\)\\|od\\(\\|ulo\\)\\|vbits\\)\\|n\\(earest\\|int\\|ot\\)\\|"
  320.    "p\\(ack\\|r\\(e\\(cision\\|sent\\)\\|oduct\\)\\)\\|r\\(a\\(dix\\|n\\("
  321.    "dom_\\(number\\|seed\\)\\|ge\\)\\)\\|e\\(peat\\|shape\\)\\|rspacing\\)\\|"
  322.    "s\\(ca\\(le\\|n\\)\\|e\\(lected_\\(int_kind\\|real_kind\\)\\|"
  323.    "t_exponent\\)\\|hape\\|i\\(gn\\|nh?\\|ze\\)\\|p\\(acing\\|read\\)\\|"
  324.    "qrt\\|um\\|ystem_clock\\)\\|t\\(anh?\\|iny\\|r\\(ans\\(fer\\|pose\\)\\|"
  325.    "im\\)\\)\\|u\\(bound\\|npack\\)\\|verify\\)[ \t]*(")
  326.   "Regexp whose first part matches F90 intrinsic procedures.")
  327.  
  328. (defconst f90-operators-re
  329. ;; "and" "or" "not" "eqv" "neqv" "eq" "ne" "lt" "le" "gt" "ge" "true" "false"
  330.  (concat
  331.    "\\.\\(and\\|eqv?\\|false\\|g[et]\\|l[et]\\|n\\(e\\(\\|qv\\)\\|"
  332.    "ot\\)\\|or\\|true\\)\\.")
  333.   "Regexp matching intrinsic operators.")
  334.  
  335. (defconst f90-hpf-keywords-re
  336.   ;; Intrinsic procedures
  337.   ;; ("all_prefix" "all_scatter" "all_suffix" "any_prefix" "any_scatter"
  338.   ;; "any_suffix" "copy_prefix" "copy_scatter" "copy_suffix" "count_prefix"
  339.   ;; "count_scatter" "count_suffix" "grade_down" "grade_up" "hpf_alignment"
  340.   ;; "hpf_template" "hpf_distribution" "iall" "iall_prefix" "iall_scatter"
  341.   ;; "iall_suffix" "iany" "iany_prefix" "iany_scatter" "iany_suffix" "iparity"
  342.   ;; "iparity_prefix" "iparity_scatter" "iparity_suffix" "leadz"
  343.   ;; "maxval_prefix" "maxval_scatter" "maxval_suffix" "minval_prefix"
  344.   ;; "minval_scatter" "minval_suffix" "parity" "parity_prefix"
  345.   ;; "parity_scatter" "parity_suffix" "popcnt" "poppar" "product_prefix"
  346.   ;; "product_scatter" "product_suffix" "sum_prefix" "sum_scatter"
  347.   ;; "sum_suffix" "ilen" "number_of_processors" "processors_shape")
  348.   ;; Directives
  349.   ;; ("align" "distribute" "dynamic" "inherit" "template" "processors"
  350.   ;; "realign" "redistribute" "independent")
  351.   ;; Keywords
  352.   ;; ("pure" "extrinsic" "new" "with" "onto" "block" "cyclic")
  353.   (concat
  354.    "\\<\\(a\\(l\\(ign\\|l_\\(prefix\\|s\\(catter\\|uffix\\)\\)\\)\\|ny_\\("
  355.    "prefix\\|s\\(catter\\|uffix\\)\\)\\)\\|block\\|c\\(o\\(py_\\(prefix\\|"
  356.    "s\\(catter\\|uffix\\)\\)\\|unt_\\(prefix\\|s\\(catter\\|uffix\\)\\)\\)\\|"
  357.    "yclic\\)\\|d\\(istribute\\|ynamic\\)\\|extrinsic\\|grade_\\(down\\|"
  358.    "up\\)\\|hpf_\\(alignment\\|distribution\\|template\\)\\|i\\(a\\(ll\\(\\|"
  359.    "_\\(prefix\\|s\\(catter\\|uffix\\)\\)\\)\\|ny\\(\\|_\\(prefix\\|s\\("
  360.    "catter\\|uffix\\)\\)\\)\\)\\|len\\|n\\(dependent\\|herit\\)\\|parity\\(\\|"
  361.    "_\\(prefix\\|s\\(catter\\|uffix\\)\\)\\)\\)\\|leadz\\|m\\(axval_\\("
  362.    "prefix\\|s\\(catter\\|uffix\\)\\)\\|inval_\\(prefix\\|s\\(catter\\|"
  363.    "uffix\\)\\)\\)\\|n\\(ew\\|umber_of_processors\\)\\|onto\\|p\\(arity\\(\\|"
  364.    "_\\(prefix\\|s\\(catter\\|uffix\\)\\)\\)\\|op\\(cnt\\|par\\)\\|ro\\("
  365.    "cessors\\(\\|_shape\\)\\|duct_\\(prefix\\|s\\(catter\\|uffix\\)\\)\\)\\|"
  366.    "ure\\)\\|re\\(align\\|distribute\\)\\|sum_\\(prefix\\|s\\(catter\\|"
  367.    "uffix\\)\\)\\|template\\|with\\)\\>")
  368.   "Regexp for all HPF keywords, procedures and directives.")
  369.  
  370. ;; Highlighting patterns
  371.  
  372. (defvar f90-font-lock-keywords-1
  373.   (if (string-match "XEmacs" emacs-version)
  374.       (list                ; XEmacs
  375.        '("\\<\\(end[ \t]*\\(program\\|module\\|function\\|subroutine\\|type\\)\\)\\>"
  376.      1 font-lock-keyword-face)
  377.        '("\\<\\(end[ \t]*\\(program\\|module\\|function\\|subroutine\\|type\\)\\)\\>[ \t]*\\(\\sw+\\)"
  378.      3 font-lock-function-name-face)
  379.        '("\\<\\(program\\|call\\|module\\|subroutine\\|function\\|use\\)\\>"
  380.      1 font-lock-keyword-face)
  381.        '("\\<\\(program\\|call\\|module\\|subroutine\\|function\\|use\\)\\>[ \t]*\\(\\sw+\\)"
  382.      2 font-lock-function-name-face nil t)
  383.        ;; Special highlighting of "module procedure foo-list"
  384.        '("\\<\\(module[ \t]*procedure\\)\\>" 1 font-lock-keyword-face t)
  385.        ;; Highlight definition of new type
  386.        '("\\<\\(type\\)[ \t]*\\(,.*::[ \t]*\\|[ \t]+\\)\\(\\sw+\\)"
  387.      1 font-lock-keyword-face)
  388.        '("\\<\\(type\\)[ \t]*\\(,.*::[ \t]*\\|[ \t]+\\)\\(\\sw+\\)"
  389.      3 font-lock-function-name-face)
  390.        "\\<\\(\\(end[ \t]*\\)?\\(interface\\|block[ \t]*data\\)\\|contains\\)\\>")
  391.     (list                ; Emacs
  392.      '("\\<\\(end[ \t]*\\(program\\|module\\|function\\|subroutine\\|type\\)\\)\\>[ \t]*\\(\\sw+\\)?"
  393.        (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t))
  394.      '("\\<\\(program\\|call\\|module\\|subroutine\\|function\\|use\\)\\>[ \t]*\\(\\sw+\\)?"
  395.        (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t))
  396.      ;; Special highlighting of "module procedure foo-list"
  397.      '("\\<\\(module[ \t]*procedure\\)\\>" (1 font-lock-keyword-face t))
  398.      ;; Highlight definition of new type
  399.      '("\\<\\(type\\)[ \t]*\\(,.*::[ \t]*\\|[ \t]+\\)\\(\\sw+\\)"
  400.        (1 font-lock-keyword-face) (3 font-lock-function-name-face))
  401.      "\\<\\(\\(end[ \t]*\\)?\\(interface\\|block[ \t]*data\\)\\|contains\\)\\>"))
  402.   "This does fairly subdued highlighting of comments and function calls.")
  403.  
  404. (defvar f90-font-lock-keywords-2
  405.   (append f90-font-lock-keywords-1
  406.      (if (string-match "XEmacs" emacs-version)
  407.       (list                ; XEmacs
  408.        ;; Variable declarations (avoid the real function call)
  409.        '("^[ \t0-9]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|logical\\|type[ \t]*(\\sw+)\\)"
  410.      1 font-lock-type-face)
  411.        '("^[ \t0-9]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|logical\\|type[ \t]*(\\sw+)\\)\\(.*::\\|[ \t]*(.*)\\)?\\(.*\\)"
  412.      4 font-lock-doc-string-face)
  413.        ;; do, if and select constructs
  414.        '("\\<\\(end[ \t]*\\(do\\|if\\|select\\)\\)\\>"
  415.      1 font-lock-keyword-face)
  416.        '("\\<\\(end[ \t]*\\(do\\|if\\|select\\)\\)\\>\\([ \t]+\\(\\sw+\\)\\)"
  417.      3 font-lock-doc-string-face)
  418.        '("^[ \t0-9]*\\(\\(\\sw+\\)[ \t]*:[ \t]*\\)\\(\\(if\\|do\\([ \t]*while\\)?\\|select[ \t]*case\\)\\)\\>"
  419.      2 font-lock-doc-string-face)
  420.        '("^[ \t0-9]*\\(\\(\\sw+\\)[ \t]*:[ \t]*\\)?\\(\\(if\\|do\\([ \t]*while\\)?\\|select[ \t]*case\\)\\)\\>"
  421.      3 font-lock-keyword-face)
  422.       ;; implicit declaration
  423.        '("\\<\\(implicit\\)[ \t]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|logical\\|type[ \t]*(\\sw+)\\|none\\)\\>"
  424.      1 font-lock-keyword-face)
  425.        '("\\<\\(implicit\\)[ \t]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|logical\\|type[ \t]*(\\sw+)\\|none\\)\\>"
  426.      2 font-lock-type-face)
  427.        '("\\<\\(namelist\\|common\\)[ \t]*\/\\(\\sw+\\)?\/"
  428.      1 font-lock-keyword-face)
  429.        '("\\<\\(namelist\\|common\\)[ \t]*\/\\(\\sw+\\)\/"
  430.      2 font-lock-doc-string-face nil t)
  431.        '("\\<\\(where\\|forall\\)[ \t]*(" . 1)
  432.        "\\<e\\(lse\\([ \t]*if\\|where\\)?\\|nd[ \t]*\\(where\\|forall\\)\\)\\>"
  433.        "\\<\\(then\\|continue\\|format\\|include\\|stop\\|return\\)\\>"
  434.        '("\\<\\(exit\\|cycle\\)\\>" 
  435.      1 font-lock-keyword-face)
  436.        '("\\<\\(exit\\|cycle\\)[ \t]*\\(\\sw+\\)2\\>" 
  437.      2 font-lock-doc-string-face)
  438.        '("\\<\\(case\\)[ \t]*\\(default\\|(\\)" . 1)
  439.        '("\\<\\(do\\|go *to\\)\\>[ \t]*\\([0-9]+\\)"
  440.      1 font-lock-keyword-face)
  441.        '("\\<\\(do\\|go *to\\)\\>[ \t]*\\([0-9]+\\)"
  442.      2 font-lock-doc-string-face)
  443.        '("^[ \t]*\\([0-9]+\\)" 1 font-lock-doc-string-face t))
  444.       (list                ; Emacs
  445.        ;; Variable declarations (avoid the real function call)
  446.        '("^[ \t0-9]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|logical\\|type[ \t]*(\\sw+)\\)\\(.*::\\|[ \t]*(.*)\\)?\\(.*\\)"
  447.      (1 font-lock-type-face) (4 font-lock-variable-name-face))
  448.        ;; do, if and select constructs
  449.        '("\\<\\(end[ \t]*\\(do\\|if\\|select\\)\\)\\>\\([ \t]+\\(\\sw+\\)\\)?"
  450.      (1 font-lock-keyword-face) (3 font-lock-reference-face nil t))
  451.        '("^[ \t0-9]*\\(\\(\\sw+\\)[ \t]*:[ \t]*\\)?\\(\\(if\\|do\\([ \t]*while\\)?\\|select[ \t]*case\\)\\)\\>"
  452.      (2 font-lock-reference-face nil t) (3 font-lock-keyword-face))
  453.        ;; implicit declaration
  454.        '("\\<\\(implicit\\)[ \t]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|logical\\|type[ \t]*(\\sw+)\\|none\\)\\>" (1 font-lock-keyword-face) (2 font-lock-type-face))
  455.        '("\\<\\(namelist\\|common\\)[ \t]*\/\\(\\sw+\\)?\/" (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
  456.        '("\\<\\(where\\|forall\\)[ \t]*(" . 1)
  457.        "\\<e\\(lse\\([ \t]*if\\|where\\)?\\|nd[ \t]*\\(where\\|forall\\)\\)\\>"
  458.        "\\<\\(then\\|continue\\|format\\|include\\|stop\\|return\\)\\>"
  459.        '("\\<\\(exit\\|cycle\\)[ \t]*\\(\\sw+\\)?\\>" 
  460.      (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
  461.        '("\\<\\(case\\)[ \t]*\\(default\\|(\\)" . 1)
  462.        '("\\<\\(do\\|go *to\\)\\>[ \t]*\\([0-9]+\\)"
  463.      (1 font-lock-keyword-face) (2 font-lock-reference-face))
  464.        '("^[ \t]*\\([0-9]+\\)" (1 font-lock-reference-face t)))))
  465.   "Highlights declarations, do-loops and other constructions")
  466.  
  467. (defvar f90-font-lock-keywords-3
  468.   (append f90-font-lock-keywords-2
  469.    (list
  470.     f90-keywords-level-3-re
  471.     f90-operators-re
  472.     (if (string-match "XEmacs" emacs-version)
  473.     (append (list f90-procedures-re) '(1 font-lock-keyword-face t))
  474.       (list f90-procedures-re '(1 font-lock-keyword-face t)))
  475.     "\\<real\\>"            ; Avoid overwriting real defs.
  476.     ))
  477.   "Highlights all F90 keywords and intrinsic procedures.")
  478.  
  479. (defvar f90-font-lock-keywords-4
  480.   (append f90-font-lock-keywords-3
  481.     (list f90-hpf-keywords-re))
  482.   "Highlights all F90 and HPF keywords.")
  483.  
  484. (defvar f90-font-lock-keywords
  485.       f90-font-lock-keywords-2
  486.   "*Default expressions to highlight in F90 mode.")
  487.  
  488. ;; syntax table
  489. (defvar f90-mode-syntax-table nil
  490.   "Syntax table in use in F90 mode buffers.")
  491.  
  492. (if f90-mode-syntax-table
  493.     ()
  494.   (setq f90-mode-syntax-table (make-syntax-table))
  495.   (modify-syntax-entry ?\! "<"  f90-mode-syntax-table) ; beg. comment
  496.   (modify-syntax-entry ?\n ">"  f90-mode-syntax-table) ; end comment
  497.   (modify-syntax-entry ?\' "\"" f90-mode-syntax-table) ; string quote
  498.   (modify-syntax-entry ?\" "\"" f90-mode-syntax-table) ; string quote
  499.   (modify-syntax-entry ?\` "w"  f90-mode-syntax-table) ; for abbrevs
  500.   (modify-syntax-entry ?\r " "  f90-mode-syntax-table) ; return is whitespace
  501.   (modify-syntax-entry ?+  "."  f90-mode-syntax-table)  
  502.   (modify-syntax-entry ?-  "."  f90-mode-syntax-table)
  503.   (modify-syntax-entry ?=  "."  f90-mode-syntax-table)
  504.   (modify-syntax-entry ?*  "."  f90-mode-syntax-table)
  505.   (modify-syntax-entry ?/  "."  f90-mode-syntax-table)
  506.   (modify-syntax-entry ?\\ "/"  f90-mode-syntax-table)
  507.   (modify-syntax-entry ?.  "."  f90-mode-syntax-table)
  508.   (modify-syntax-entry ?%  "."  f90-mode-syntax-table) ; not in f77
  509.   (modify-syntax-entry ?$  "_"  f90-mode-syntax-table)
  510.   (modify-syntax-entry ?@  "_"  f90-mode-syntax-table)
  511.   (modify-syntax-entry ?_  "_"  f90-mode-syntax-table)
  512.   ) ; escape chars
  513.  
  514. ;; keys
  515. (defvar f90-mode-map ()
  516.   "Keymap used in F90 mode.")
  517.  
  518. (if f90-mode-map
  519.     ()
  520.   (setq f90-mode-map (make-sparse-keymap))
  521.   (define-key f90-mode-map "`"        'f90-abbrev-start)
  522.   (define-key f90-mode-map "\C-c;"    'f90-comment-region)
  523.   (define-key f90-mode-map "\C-\M-a"  'f90-beginning-of-subprogram)
  524.   (define-key f90-mode-map "\C-\M-e"  'f90-end-of-subprogram)
  525.   (define-key f90-mode-map "\C-\M-h"  'f90-mark-subprogram)
  526.   (define-key f90-mode-map "\C-\M-q"  'f90-indent-subprogram)
  527.   (define-key f90-mode-map "\C-j"     'f90-indent-new-line) ; LFD equals C-j
  528.   (define-key f90-mode-map "\r"       'newline)
  529.   (define-key f90-mode-map "\C-c\r"   'f90-break-line)
  530.   ;;  (define-key f90-mode-map [M-return] 'f90-break-line)
  531.   (define-key f90-mode-map "\C-c\C-d" 'f90-join-lines)
  532.   (define-key f90-mode-map "\C-c\C-f" 'f90-fill-region)
  533.   (define-key f90-mode-map "\C-c\C-p" 'f90-previous-statement)
  534.   (define-key f90-mode-map "\C-c\C-n" 'f90-next-statement)
  535.   (define-key f90-mode-map "\C-c\C-w" 'f90-insert-end)
  536.   (define-key f90-mode-map "\t"       'f90-indent-line))
  537.  
  538. ;; menus
  539. (if (string-match "XEmacs" emacs-version)
  540.     (defvar f90-xemacs-menu
  541.       '("F90"
  542.     ["Indent Subprogram"       f90-indent-subprogram t]
  543.     ["Mark Subprogram"         f90-mark-subprogram t]
  544.     ["Beginning of Subprogram" f90-beginning-of-subprogram t]
  545.     ["End of Subprogram"       f90-end-of-subprogram t]
  546.     "-----"
  547.     ["(Un)Comment Region"      f90-comment-region t]
  548.     ["Indent Region"           indent-region t]
  549.     ["Fill Region"             f90-fill-region t]
  550.     "-----"
  551.     ["Break Line at Point"     f90-break-line t]
  552.     ["Join with Next Line"     f90-join-lines t]
  553.     ["Insert Newline"          newline t]
  554.     ["Insert End"              f90-insert-end t]
  555.     "-----"
  556.     ["Upcase Keywords (buffer)"      f90-upcase-keywords t]
  557.     ["Upcase Keywords (region)"      f90-upcase-region-keywords
  558.      t]
  559.     ["Capitalize Keywords (buffer)"  f90-capitalize-keywords t]
  560.     ["Capitalize Keywords (region)" 
  561.      f90-capitalize-region-keywords t]
  562.     ["Downcase Keywords (buffer)"    f90-downcase-keywords t]
  563.     ["Downcase Keywords (region)"   
  564.      f90-downcase-region-keywords t]
  565.     "-----"
  566.     ["Toggle abbrev-mode"   abbrev-mode             t]
  567.     ["Toggle auto-fill"     f90-auto-fill-mode      t])
  568.       "XEmacs menu for F90 mode.")
  569.   ;; Emacs
  570.   (define-key f90-mode-map [menu-bar] (make-sparse-keymap))
  571.   (define-key f90-mode-map [menu-bar f90] 
  572.     (cons "F90" (make-sparse-keymap "f90"))) 
  573.   (define-key f90-mode-map [menu-bar f90 abbrev-mode]
  574.     '("Toggle abbrev-mode" . abbrev-mode))
  575.   (define-key f90-mode-map [menu-bar f90 f90-auto-fill-mode]
  576.     '("Toggle auto-fill" . f90-auto-fill-mode))
  577.   (define-key f90-mode-map [menu-bar f90 f90-downcase-region-keywords]
  578.     '("Downcase Keywords (region)" . f90-downcase-region-keywords))
  579.   (define-key f90-mode-map [menu-bar f90 f90-downcase-keywords]
  580.     '("Downcase Keywords (buffer)" . f90-downcase-keywords))
  581.   (define-key f90-mode-map [menu-bar f90 f90-capitalize-keywords]
  582.     '("Capitalize Keywords (region)" . f90-capitalize-region-keywords))
  583.   (define-key f90-mode-map [menu-bar f90 f90-capitalize-region-keywords]
  584.     '("Capitalize Keywords (buffer)" . f90-capitalize-keywords))
  585.   (define-key f90-mode-map [menu-bar f90 f90-upcase-region-keywords]
  586.     '("Upcase Keywords (region)" . f90-upcase-region-keywords))
  587.   (define-key f90-mode-map [menu-bar f90 f90-upcase-keywords]
  588.     '("Upcase Keywords (buffer)" . f90-upcase-keywords))
  589.   (define-key f90-mode-map [menu-bar f90 f90-insert-end]
  590.     '("Insert end" . f90-insert-end))
  591.   (define-key f90-mode-map [menu-bar f90 f90-join-lines]
  592.     '("Join with Next Line" . f90-join-lines))
  593.   (define-key f90-mode-map [menu-bar f90 f90-break-line]
  594.     '("Break Line at Point" . f90-break-line))
  595.   (define-key f90-mode-map [menu-bar f90 f90-fill-region]
  596.     '("Fill Region" . f90-fill-region))
  597.   (define-key f90-mode-map [menu-bar f90 indent-region]
  598.     '("Indent Region" . indent-region))
  599.   (define-key f90-mode-map [menu-bar f90 f90-comment-region]
  600.     '("(Un)Comment Region" . f90-comment-region))
  601.   (define-key f90-mode-map [menu-bar f90 f90-end-of-subprogram]
  602.     '("End of Subprogram" . f90-end-of-subprogram))
  603.   (define-key f90-mode-map [menu-bar f90 f90-beginning-of-subprogram]
  604.     '("Beginning of Subprogram" . f90-beginning-of-subprogram))
  605.   (define-key f90-mode-map [menu-bar f90 f90-mark-subprogram]
  606.     '("Mark Subprogram" . f90-mark-subprogram))
  607.   (define-key f90-mode-map [menu-bar f90 f90-indent-subprogram]
  608.     '("Indent Subprogram" . f90-indent-subprogram)))
  609.   
  610. ;; Regexps for finding program structures.
  611. (defconst f90-blocks-re 
  612.   "\\(block[ \t]*data\\|do\\|if\\|interface\\|function\\|module\\|\
  613. program\\|select\\|subroutine\\|type\\|where\\|forall\\)\\>")
  614. (defconst f90-program-block-re 
  615.   "\\(program\\|module\\|subroutine\\|function\\)")
  616. (defconst f90-else-like-re 
  617.   "\\(else\\([ \t]*if\\|where\\)?\\|case[ \t]*\\(default\\|(\\)\\)")
  618. (defconst f90-end-if-re 
  619.   "end[ \t]*\\(if\\|select\\|where\\|forall\\)\\>")
  620. (defconst f90-end-type-re 
  621.   "end[ \t]*\\(type\\|interface\\|block[ \t]*data\\)")
  622. (defconst f90-type-def-re
  623.   "\\<\\(type\\)[ \t]*\\(,.*::[ \t]*\\|[ \t]+\\)\\(\\sw+\\)")
  624. (defconst f90-no-break-re  "\\(\\*\\*\\|//\\|=>\\)")
  625. ;; A temporary position to make region operators faster
  626. (defvar f90-cache-position nil)
  627. (make-variable-buffer-local 'f90-cache-position)
  628.  
  629. ;; Imenu support
  630. (defvar f90-imenu-generic-expression
  631.   (cons
  632.    (concat
  633.     "^[ \t0-9]*\\("
  634.     "program[ \t]+\\(\\sw+\\)\\|"
  635.     "module[ \t]+\\(\\sw+\\)[ \t]*\\(!\\|$\\)\\|"
  636.     "\\(pure\\|recursive\\|extrinsic([^)]+)\\)?[ \t]*"
  637.     "subroutine[ \t]+\\(\\sw+\\)\\|"
  638.     ; avoid end function, but allow for most other things
  639.     "\\([^!]*\\([^e!].[^ \t!]\\|.[^n!][^ \t!]\\|..[^d \t!]\\)"
  640.     "\\|[^!]?[^!]?\\)[ \t]*function[ \t]+\\(\\sw+\\)"
  641.     "\\)")
  642.    (list 2 3 6 9))
  643.   "imenu generic expression for F90 mode.")
  644.  
  645. ;; When compiling under GNU Emacs, load imenu during compilation.  If
  646. ;; you have 19.22 or earlier, comment this out, or get imenu.
  647. (and (fboundp 'eval-when-compile)
  648.      (eval-when-compile
  649.        (if (not (string-match "XEmacs" emacs-version))
  650.        (require 'imenu))
  651.        ()))
  652.  
  653.  
  654.  
  655. ;; abbrevs have generally two letters, except standard types `c, `i, `r, `t
  656. (defvar f90-mode-abbrev-table nil)
  657. (if f90-mode-abbrev-table
  658.     ()
  659.   (let ((ac abbrevs-changed))
  660.     (define-abbrev-table 'f90-mode-abbrev-table ())
  661.     (define-abbrev f90-mode-abbrev-table  "`al"  "allocate" nil)
  662.     (define-abbrev f90-mode-abbrev-table  "`ab"  "allocatable" nil)
  663.     (define-abbrev f90-mode-abbrev-table  "`as"  "assignment" nil)
  664.     (define-abbrev f90-mode-abbrev-table  "`ba"  "backspace" nil)
  665.     (define-abbrev f90-mode-abbrev-table  "`bd"  "block data" nil)
  666.     (define-abbrev f90-mode-abbrev-table  "`c"   "character" nil)
  667.     (define-abbrev f90-mode-abbrev-table  "`cl"  "close" nil)
  668.     (define-abbrev f90-mode-abbrev-table  "`cm"  "common" nil)
  669.     (define-abbrev f90-mode-abbrev-table  "`cx"  "complex" nil)
  670.     (define-abbrev f90-mode-abbrev-table  "`cn"  "contains" nil)
  671.     (define-abbrev f90-mode-abbrev-table  "`cy"  "cycle" nil)
  672.     (define-abbrev f90-mode-abbrev-table  "`de"  "deallocate" nil)
  673.     (define-abbrev f90-mode-abbrev-table  "`df"  "define" nil)
  674.     (define-abbrev f90-mode-abbrev-table  "`di"  "dimension" nil)
  675.     (define-abbrev f90-mode-abbrev-table  "`dw"  "do while" nil)
  676.     (define-abbrev f90-mode-abbrev-table  "`el"  "else" nil)
  677.     (define-abbrev f90-mode-abbrev-table  "`eli" "else if" nil)
  678.     (define-abbrev f90-mode-abbrev-table  "`elw" "elsewhere" nil)
  679.     (define-abbrev f90-mode-abbrev-table  "`eq"  "equivalence" nil)
  680.     (define-abbrev f90-mode-abbrev-table  "`ex"  "external" nil)
  681.     (define-abbrev f90-mode-abbrev-table  "`ey"  "entry" nil)
  682.     (define-abbrev f90-mode-abbrev-table  "`fl"  "forall" nil)
  683.     (define-abbrev f90-mode-abbrev-table  "`fo"  "format" nil)
  684.     (define-abbrev f90-mode-abbrev-table  "`fu"  "function" nil)
  685.     (define-abbrev f90-mode-abbrev-table  "`fa"  ".false." nil)
  686.     (define-abbrev f90-mode-abbrev-table  "`im"  "implicit none" nil)
  687.     (define-abbrev f90-mode-abbrev-table  "`in " "include" nil)
  688.     (define-abbrev f90-mode-abbrev-table  "`i"   "integer" nil)
  689.     (define-abbrev f90-mode-abbrev-table  "`it"  "intent" nil)
  690.     (define-abbrev f90-mode-abbrev-table  "`if"  "interface" nil)
  691.     (define-abbrev f90-mode-abbrev-table  "`lo"  "logical" nil)
  692.     (define-abbrev f90-mode-abbrev-table  "`mo"  "module" nil)
  693.     (define-abbrev f90-mode-abbrev-table  "`na"  "namelist" nil)
  694.     (define-abbrev f90-mode-abbrev-table  "`nu"  "nullify" nil)
  695.     (define-abbrev f90-mode-abbrev-table  "`op"  "optional" nil)
  696.     (define-abbrev f90-mode-abbrev-table  "`pa"  "parameter" nil)
  697.     (define-abbrev f90-mode-abbrev-table  "`po"  "pointer" nil)
  698.     (define-abbrev f90-mode-abbrev-table  "`pr"  "print" nil)
  699.     (define-abbrev f90-mode-abbrev-table  "`pi"  "private" nil)
  700.     (define-abbrev f90-mode-abbrev-table  "`pm"  "program" nil)
  701.     (define-abbrev f90-mode-abbrev-table  "`pu"  "public" nil)
  702.     (define-abbrev f90-mode-abbrev-table  "`r"   "real" nil)
  703.     (define-abbrev f90-mode-abbrev-table  "`rc"  "recursive" nil)
  704.     (define-abbrev f90-mode-abbrev-table  "`rt"  "return" nil)
  705.     (define-abbrev f90-mode-abbrev-table  "`rw"  "rewind" nil)
  706.     (define-abbrev f90-mode-abbrev-table  "`se"  "select" nil)
  707.     (define-abbrev f90-mode-abbrev-table  "`sq"  "sequence" nil)
  708.     (define-abbrev f90-mode-abbrev-table  "`su"  "subroutine" nil)
  709.     (define-abbrev f90-mode-abbrev-table  "`ta"  "target" nil)
  710.     (define-abbrev f90-mode-abbrev-table  "`tr"  ".true." nil)
  711.     (define-abbrev f90-mode-abbrev-table  "`t"   "type" nil)
  712.     (define-abbrev f90-mode-abbrev-table  "`wh"  "where" nil)
  713.     (define-abbrev f90-mode-abbrev-table  "`wr"  "write" nil)
  714.     (setq abbrevs-changed ac)))
  715.  
  716. ;;;###autoload
  717. (defun f90-mode ()
  718.   "Major mode for editing Fortran 90 code in free format.
  719.  
  720. \\[f90-indent-new-line] corrects current indentation and creates new\
  721.  indented line.
  722. \\[f90-indent-line] indents the current line correctly. 
  723. \\[f90-indent-subprogram] indents the current subprogram. 
  724.  
  725. Type `? or `\\[help-command] to display a list of built-in\
  726.  abbrevs for F90 keywords.
  727.  
  728. Key definitions:
  729. \\{f90-mode-map}
  730.  
  731. Variables controlling indentation style and extra features:
  732.  
  733.  f90-do-indent
  734.     Extra indentation within do blocks.  (default 3)
  735.  f90-if-indent
  736.     Extra indentation within if/select case/where/forall blocks. (default 3)
  737.  f90-type-indent
  738.     Extra indentation within type/interface/block-data blocks.  (default 3)
  739.  f90-program-indent
  740.     Extra indentation within program/module/subroutine/function blocks.
  741.       (default 2)
  742.  f90-continuation-indent
  743.     Extra indentation applied to continuation lines.  (default 5)
  744.  f90-comment-region
  745.     String inserted by \\[f90-comment-region] at start of each line in 
  746.     region.  (default \"!!!$\")
  747.  f90-indented-comment-re
  748.     Regexp determining the type of comment to be intended like code.
  749.     (default \"!\")
  750.  f90-directive-comment-re
  751.     Regexp of comment-like directive like \"!HPF\\\\$\", not to be indented.
  752.     (default \"!hpf\\\\$\")
  753.  f90-break-delimiters
  754.     Regexp holding list of delimiters at which lines may be broken.
  755.     (default \"[-+*/><=,% \\t]\")
  756.  f90-break-before-delimiters
  757.     Non-nil causes `f90-do-auto-fill' to break lines before delimiters.
  758.     (default t)
  759.  f90-beginning-ampersand 
  760.     Automatic insertion of \& at beginning of continuation lines. (default t)
  761.  f90-smart-end 
  762.     From an END statement, check and fill the end using matching block start.
  763.     Allowed values are 'blink, 'no-blink, and nil, which determine
  764.     whether to blink the matching beginning.) (default 'blink)
  765.  f90-auto-keyword-case
  766.     Automatic change of case of keywords. (default nil)
  767.     The possibilities are 'downcase-word, 'upcase-word, 'capitalize-word.
  768.  f90-leave-line-no
  769.     Do not left-justify line numbers. (default nil)
  770.  f90-startup-message
  771.     Set to nil to inhibit message first time F90 mode is used. (default t)
  772.  f90-keywords-re
  773.     List of keywords used for highlighting/upcase-keywords etc.
  774.  
  775. Turning on F90 mode calls the value of the variable `f90-mode-hook'
  776. with no args, if that value is non-nil."
  777.   (interactive)
  778.   (kill-all-local-variables)
  779.   (setq major-mode 'f90-mode)
  780.   (setq mode-name "F90")
  781.   (setq local-abbrev-table f90-mode-abbrev-table)
  782.   (set-syntax-table f90-mode-syntax-table)
  783.   (use-local-map f90-mode-map)
  784.   (make-local-variable 'indent-line-function)
  785.   (setq indent-line-function 'f90-indent-line)
  786.   (make-local-variable 'indent-region-function)
  787.   (setq indent-region-function 'f90-indent-region)
  788.   (make-local-variable 'require-final-newline)
  789.   (setq require-final-newline t)
  790.   (make-local-variable 'comment-start)
  791.   (setq comment-start "!")
  792.   (make-local-variable 'comment-start-skip)
  793.   (setq comment-start-skip "!+ *")
  794.   (make-local-variable 'comment-indent-function)
  795.   (setq comment-indent-function 'f90-comment-indent)
  796.   (make-local-variable 'abbrev-all-caps)
  797.   (setq abbrev-all-caps t)
  798.   (setq indent-tabs-mode nil)
  799.   ;; Setting up things for font-lock
  800.   (if (string-match "XEmacs" emacs-version)
  801.       (progn
  802.     (put 'f90-mode 'font-lock-keywords-case-fold-search t)
  803.     (if (and (featurep 'menubar)
  804.          current-menubar
  805.          (not (assoc "F90" current-menubar)))
  806.         (progn
  807.           (set-buffer-menubar (copy-sequence current-menubar))
  808.           (add-submenu nil f90-xemacs-menu)))))
  809.   ;; XEmacs: (Don't need a special case, since both emacsen work alike -sb)
  810.   (make-local-variable 'font-lock-defaults)
  811.   (setq font-lock-defaults 
  812.     '((f90-font-lock-keywords f90-font-lock-keywords-1
  813.                   f90-font-lock-keywords-2
  814.                   f90-font-lock-keywords-3
  815.                   f90-font-lock-keywords-4)
  816.       nil t))
  817.   ;; Tell imenu how to handle f90.
  818.   (make-local-variable 'imenu-generic-expression)
  819.   (setq imenu-generic-expression f90-imenu-generic-expression)
  820.   (run-hooks 'f90-mode-hook)
  821.   (if f90-startup-message
  822.       (message "Emacs F90 mode; please report bugs to %s" bug-f90-mode))
  823.   (setq f90-startup-message nil))
  824.  
  825. ;; inline-functions
  826. (defsubst f90-get-beg-of-line ()
  827.   (save-excursion (beginning-of-line) (point)))
  828.  
  829. (defsubst f90-get-end-of-line ()
  830.   (save-excursion (end-of-line) (point)))
  831.  
  832. (defsubst f90-in-string ()
  833.   (let ((beg-pnt
  834.      (if (and f90-cache-position (> (point) f90-cache-position))
  835.          f90-cache-position
  836.        (point-min))))
  837.     (nth 3 (parse-partial-sexp beg-pnt (point)))))
  838.         
  839. (defsubst f90-in-comment ()
  840.   (let ((beg-pnt
  841.      (if (and f90-cache-position (> (point) f90-cache-position))
  842.          f90-cache-position
  843.        (point-min))))
  844.     (nth 4 (parse-partial-sexp beg-pnt (point)))))
  845.  
  846. (defsubst f90-line-continued ()
  847.   (save-excursion
  848.     (let ((bol (f90-get-beg-of-line)))
  849.       (end-of-line)
  850.       (while (f90-in-comment)
  851.     (search-backward "!" bol)
  852.     (skip-chars-backward "!"))
  853.       (skip-chars-backward " \t")
  854.       (= (preceding-char) ?&))))
  855.  
  856. (defsubst f90-current-indentation ()
  857.   "Return indentation of current line.
  858. Line-numbers are considered whitespace characters."
  859.   (save-excursion
  860.     (beginning-of-line) (skip-chars-forward " \t0-9")
  861.     (current-column)))
  862.  
  863. (defsubst f90-indent-to (col &optional no-line-number)
  864.   "Indent current line to column COL.
  865. If no-line-number nil, jump over a possible line-number."
  866.   (beginning-of-line)
  867.   (if (not no-line-number)
  868.       (skip-chars-forward " \t0-9"))
  869.   (delete-horizontal-space)
  870.   (if (zerop (current-column))
  871.       (indent-to col)
  872.     (indent-to col 1)))
  873.  
  874. (defsubst f90-match-piece (arg)
  875.   (if (match-beginning arg)
  876.       (buffer-substring (match-beginning arg) (match-end arg))))
  877.  
  878. (defsubst f90-get-present-comment-type ()
  879.   (save-excursion
  880.     (let ((type nil) (eol (f90-get-end-of-line)))
  881.       (if (f90-in-comment)
  882.       (progn
  883.         (beginning-of-line)
  884.         (re-search-forward "[!]+" eol)
  885.         (while (f90-in-string)
  886.           (re-search-forward "[!]+" eol))
  887.         (setq type (buffer-substring (match-beginning 0) (match-end 0)))))
  888.       type)))
  889.  
  890. (defsubst f90-equal-symbols (a b)
  891.   "Compare strings neglecting case and allowing for nil value."
  892.   (let ((a-local (if a (downcase a) nil))
  893.     (b-local (if b (downcase b) nil)))
  894.     (equal a-local b-local)))
  895.  
  896. ;; XEmacs 19.11 & 19.12 gives back a single char when matching an empty regular
  897. ;; expression. Therefore, the next 2 functions are longer than necessary.
  898.  
  899. (defsubst f90-looking-at-do ()
  900.   "Return (\"do\" name) if a do statement starts after point.
  901. Name is nil if the statement has no label."
  902.   (if (looking-at "\\(\\(\\sw+\\)[ \t]*\:\\)?[ \t]*\\(do\\)\\>")
  903.       (let (label
  904.         (struct (f90-match-piece 3)))
  905.     (if (looking-at "\\(\\sw+\\)[ \t]*\:")
  906.         (setq label (f90-match-piece 1)))
  907.     (list struct label))))
  908.  
  909. (defsubst f90-looking-at-select-case ()
  910.   "Return (\"select\" name) if a select-case statement starts after point.
  911. Name is nil if the statement has no label."
  912.   (if (looking-at "\\(\\(\\sw+\\)[ \t]*\:\\)?[ \t]*\\(select\\)[ \t]*case[ \t]*(")
  913.       (let (label
  914.         (struct (f90-match-piece 3)))
  915.     (if (looking-at "\\(\\sw+\\)[ \t]*\:")
  916.         (setq label (f90-match-piece 1)))
  917.     (list struct label))))
  918.  
  919. (defsubst f90-looking-at-if-then ()
  920.   "Return (\"if\" name) if an if () then statement starts after point.
  921. Name is nil if the statement has no label."
  922.   (save-excursion
  923.     (let (struct (label nil))
  924.       (if (looking-at "\\(\\(\\sw+\\)[ \t]*\:\\)?[ \t]*\\(if\\)\\>")
  925.       (progn
  926.         (setq struct (f90-match-piece 3))
  927.         (if (looking-at "\\(\\sw+\\)[ \t]*\:")
  928.         (setq label (f90-match-piece 1)))
  929.         (goto-char (scan-lists (point) 1 0))
  930.         (skip-chars-forward " \t")
  931.         (if (or (looking-at "then\\>")
  932.             (if (f90-line-continued)
  933.             (progn
  934.               (f90-next-statement)
  935.               (skip-chars-forward " \t0-9&")
  936.               (looking-at "then\\>"))))
  937.         (list struct label)))))))
  938.  
  939. (defsubst f90-looking-at-where-or-forall ()
  940.   "Return (kind nil) if where/forall...end starts after point."
  941.   (save-excursion
  942.     (let (command)
  943.       (if (looking-at "\\(where\\|forall\\)[ \t]*(")
  944.       (progn
  945.         (setq command (list (f90-match-piece 1) nil))
  946.         (goto-char (scan-lists (point) 1 0))
  947.         (skip-chars-forward " \t")
  948.         (if (looking-at "\\(!\\|$\\)")
  949.         command))))))
  950.  
  951. (defsubst f90-looking-at-type-like ()
  952.   "Return (kind name) at the start of a type/interface/block-data block.
  953. Name is non-nil only for type."
  954.   (cond 
  955.    ((looking-at f90-type-def-re)
  956.     (list (f90-match-piece 1) (f90-match-piece 3)))
  957.    ((looking-at "\\(interface\\|block[\t]*data\\)\\>")
  958.     (list (f90-match-piece 1) nil))))
  959.  
  960. (defsubst f90-looking-at-program-block-start ()
  961.   "Return (kind name) if a program block with name name starts after point."
  962.   (cond
  963.    ((looking-at "\\(program\\)[ \t]+\\(\\sw+\\)\\>")
  964.     (list (f90-match-piece 1) (f90-match-piece 2)))
  965.    ((and (not (looking-at "module[ \t]*procedure\\>"))
  966.      (looking-at "\\(module\\)[ \t]+\\(\\sw+\\)\\>"))
  967.     (list (f90-match-piece 1) (f90-match-piece 2)))
  968.    ((looking-at (concat
  969.          "\\(pure\\|recursive\\|extrinsic([^)]+)\\)?[ \t]*"
  970.          "\\(subroutine\\)[ \t]+\\(\\sw+\\)"))
  971.     (list (f90-match-piece 2) (f90-match-piece 3)))
  972.    ((and (not (looking-at "end[ \t]*function"))
  973.      (looking-at "[^!\"\&\\n]*\\(function\\)[ \t]+\\(\\sw+\\)"))
  974.     (list (f90-match-piece 1) (f90-match-piece 2)))))
  975.  
  976. (defsubst f90-looking-at-program-block-end ()
  977.   "Return list of type and name of end of block."
  978.   (if (looking-at (concat "end[ \t]*" f90-blocks-re 
  979.               "?\\([ \t]+\\(\\sw+\\)\\)?\\>"))
  980.       (list (f90-match-piece 1) (f90-match-piece 3))))
  981.  
  982. (defsubst f90-comment-indent ()
  983.   (cond ((looking-at "!!!") 0)
  984.     ((and f90-directive-comment-re
  985.           (looking-at f90-directive-comment-re)) 0)
  986.     ((looking-at (regexp-quote f90-comment-region)) 0)
  987.     ((looking-at f90-indented-comment-re)
  988.      (f90-calculate-indent))
  989.     (t (skip-chars-backward " \t")
  990.        (max (if (bolp) 0 (1+ (current-column))) comment-column))))
  991.  
  992. (defsubst f90-present-statement-cont ()
  993.   "Return continuation properties of present statement."
  994.   (let (pcont cont)
  995.     (save-excursion
  996.       (setq pcont (if (f90-previous-statement) (f90-line-continued) nil)))
  997.     (setq cont (f90-line-continued))
  998.     (cond ((and (not pcont) (not cont)) 'single)
  999.        ((and (not pcont) cont)       'begin)
  1000.        ((and pcont       (not cont)) 'end)
  1001.        ((and pcont       cont)       'middle)
  1002.        (t (error)))))
  1003.  
  1004. (defsubst f90-indent-line-no ()
  1005.   (if f90-leave-line-no
  1006.       ()
  1007.     (if (and (not (zerop (skip-chars-forward " \t")))
  1008.          (looking-at "[0-9]"))
  1009.     (delete-horizontal-space)))
  1010.   (skip-chars-forward " \t0-9"))
  1011.  
  1012. (defsubst f90-no-block-limit ()
  1013.   (let ((eol (f90-get-end-of-line)))
  1014.     (save-excursion
  1015.       (not (or (looking-at "end")
  1016.            (looking-at "\\(do\\|if\\|else\\|select[ \t]*case\\|\
  1017. case\\|where\\|forall\\)\\>")
  1018.            (looking-at "\\(program\\|module\\|interface\\|\
  1019. block[ \t]*data\\)\\>")
  1020.            (looking-at "\\(contains\\|\\sw+[ \t]*:\\)")
  1021.            (looking-at f90-type-def-re)
  1022.            (re-search-forward "\\(function\\|subroutine\\)" eol t))))))
  1023.  
  1024. (defsubst f90-update-line ()
  1025.   (let (bol eol)
  1026.     (if f90-auto-keyword-case
  1027.     (progn (setq bol (f90-get-beg-of-line)
  1028.              eol (f90-get-end-of-line))
  1029.            (if f90-auto-keyword-case
  1030.            (f90-change-keywords f90-auto-keyword-case bol eol))))))
  1031.  
  1032. (defun f90-get-correct-indent ()
  1033.   "Get correct indent for a line starting with line number.
  1034. Does not check type and subprogram indentation."
  1035.   (let ((epnt (f90-get-end-of-line)) icol cont)
  1036.     (save-excursion
  1037.       (while (and (f90-previous-statement)
  1038.           (or (progn
  1039.             (setq cont (f90-present-statement-cont))
  1040.             (or (eq cont 'end) (eq cont 'middle)))
  1041.               (looking-at "[ \t]*[0-9]"))))
  1042.       (setq icol (current-indentation))
  1043.       (beginning-of-line)
  1044.       (if (re-search-forward "\\(if\\|do\\|select\\|where\\|forall\\)"
  1045.                  (f90-get-end-of-line) t)
  1046.       (progn
  1047.         (beginning-of-line) (skip-chars-forward " \t")
  1048.         (cond ((f90-looking-at-do)
  1049.            (setq icol (+ icol f90-do-indent)))
  1050.           ((or (f90-looking-at-if-then)
  1051.                (f90-looking-at-where-or-forall)
  1052.                (f90-looking-at-select-case))
  1053.            (setq icol (+ icol f90-if-indent))))
  1054.         (end-of-line)))
  1055.       (while (re-search-forward
  1056.           "\\(if\\|do\\|select\\|where\\|forall\\)" epnt t)
  1057.     (beginning-of-line) (skip-chars-forward " \t0-9")
  1058.     (cond  ((f90-looking-at-do)
  1059.         (setq icol (+ icol f90-do-indent)))
  1060.            ((or (f90-looking-at-if-then)
  1061.             (f90-looking-at-where-or-forall)
  1062.             (f90-looking-at-select-case))
  1063.         (setq icol (+ icol f90-if-indent)))
  1064.            ((looking-at f90-end-if-re)
  1065.         (setq icol (- icol f90-if-indent)))
  1066.            ((looking-at "end[ \t]*do\\>")
  1067.         (setq icol (- icol f90-do-indent))))
  1068.     (end-of-line))
  1069.       icol)))
  1070.            
  1071.       
  1072. (defun f90-calculate-indent ()
  1073.   "Calculate the indent column based on previous statements."
  1074.   (interactive)
  1075.   (let (icol cont (case-fold-search t) (pnt (point)))
  1076.     (save-excursion
  1077.       (if (not (f90-previous-statement))
  1078.       (setq icol 0)
  1079.     (setq cont (f90-present-statement-cont))
  1080.     (if (eq cont 'end)
  1081.         (while (not (eq 'begin (f90-present-statement-cont)))
  1082.           (f90-previous-statement)))
  1083.     (cond ((eq cont 'begin)
  1084.            (setq icol (+ (f90-current-indentation)
  1085.                  f90-continuation-indent)))
  1086.           ((eq cont 'middle) (setq icol(current-indentation)))
  1087.           (t (setq icol (f90-current-indentation))
  1088.          (skip-chars-forward " \t")
  1089.          (if (looking-at "[0-9]")
  1090.              (setq icol (f90-get-correct-indent))
  1091.            (cond ((or (f90-looking-at-if-then)
  1092.                   (f90-looking-at-where-or-forall)
  1093.                   (f90-looking-at-select-case)
  1094.                   (looking-at f90-else-like-re))               
  1095.               (setq icol (+ icol f90-if-indent)))
  1096.              ((f90-looking-at-do)
  1097.               (setq icol (+ icol f90-do-indent)))
  1098.              ((f90-looking-at-type-like)
  1099.               (setq icol (+ icol f90-type-indent)))
  1100.              ((or (f90-looking-at-program-block-start)
  1101.                   (looking-at "contains[ \t]*\\($\\|!\\)"))
  1102.               (setq icol (+ icol f90-program-indent)))))
  1103.          (goto-char pnt)
  1104.          (beginning-of-line)
  1105.          (cond ((looking-at "[ \t]*$"))
  1106.                ((looking-at "[ \t]*#") ; Check for cpp directive.
  1107.             (setq icol 0))
  1108.                (t
  1109.             (skip-chars-forward " \t0-9")
  1110.             (cond ((or (looking-at f90-else-like-re)
  1111.                    (looking-at f90-end-if-re))
  1112.                    (setq icol (- icol f90-if-indent)))
  1113.                   ((looking-at "end[ \t]*do\\>")
  1114.                    (setq icol (- icol f90-do-indent)))
  1115.                   ((looking-at f90-end-type-re)
  1116.                    (setq icol (- icol f90-type-indent)))
  1117.                   ((or (looking-at "contains[ \t]*\\(!\\|$\\)")
  1118.                    (f90-looking-at-program-block-end))
  1119.                    (setq icol (- icol f90-program-indent))))))
  1120.          ))))
  1121.     icol))
  1122.  
  1123. ;; Statement = statement line, a line which is neither blank, nor a comment.
  1124. (defun f90-previous-statement ()
  1125.   "Move point to beginning of the previous F90 statement.
  1126. Return nil if no previous statement is found."
  1127.   (interactive)
  1128.   (let (not-first-statement)
  1129.     (beginning-of-line)
  1130.     (while (and (setq not-first-statement (zerop (forward-line -1)))
  1131.         (looking-at "[ \t0-9]*\\(!\\|$\\|#\\)")))
  1132.     not-first-statement))
  1133.  
  1134. (defun f90-next-statement ()
  1135.   "Move point to beginning of the next F90 statement.
  1136. Return nil if no later statement is found."
  1137.   (interactive)
  1138.   (let (not-last-statement)
  1139.     (beginning-of-line)
  1140.     (while (and (setq not-last-statement
  1141.               (and (zerop (forward-line 1))
  1142.                (not (eobp))))
  1143.          (looking-at "[ \t0-9]*\\(!\\|$\\)")))
  1144.     not-last-statement))
  1145.  
  1146. (defun f90-beginning-of-subprogram ()
  1147.   "Move point to the beginning of subprogram.
  1148. Return (type name) or nil if not found."
  1149.   (interactive)
  1150.   (let ((count 1) (case-fold-search t) matching-beg)
  1151.     (beginning-of-line) (skip-chars-forward " \t0-9")
  1152.     (if (setq matching-beg (f90-looking-at-program-block-start)) 
  1153.     (setq count (- count 1)))
  1154.     (while (and (not (zerop count))
  1155.         (re-search-backward f90-program-block-re nil 'move))
  1156.       (beginning-of-line) (skip-chars-forward " \t0-9")
  1157.       (cond 
  1158.        ((setq matching-beg (f90-looking-at-program-block-start))
  1159.     (setq count (- count 1)))
  1160.        ((f90-looking-at-program-block-end)
  1161.     (setq count (+ count 1)))))
  1162.     (beginning-of-line)
  1163.     (if (zerop count)
  1164.     matching-beg
  1165.       (message "No beginning-found.")
  1166.       nil)))
  1167.  
  1168. (defun f90-end-of-subprogram ()
  1169.   "Move point to the end of subprogram.
  1170. Return (type name) or nil if not found."
  1171.   (interactive)
  1172.   (let ((count 1) (case-fold-search t) matching-end)
  1173.     (beginning-of-line) (skip-chars-forward " \t0-9")
  1174.     (if (setq matching-end (f90-looking-at-program-block-end))
  1175.     (setq count (1- count)))
  1176.     (end-of-line)
  1177.     (while (and (not (zerop count))
  1178.         (re-search-forward f90-program-block-re nil 'move))
  1179.       (beginning-of-line) (skip-chars-forward " \t0-9")
  1180.       (cond ((f90-looking-at-program-block-start)
  1181.          (setq count (+ count 1)))
  1182.         ((setq matching-end (f90-looking-at-program-block-end))
  1183.          (setq count (1- count ))))
  1184.       (end-of-line))
  1185.     (forward-line 1)
  1186.     (if (zerop count)
  1187.     matching-end
  1188.       (message "No end found.")
  1189.       nil)))
  1190.  
  1191. (defun f90-mark-subprogram ()
  1192.   "Put mark at end of F90 subprogram, point at beginning.
  1193. Marks are pushed and highlight (grey shadow) is turned on."
  1194.   (interactive)
  1195.   (let ((pos (point)) program)
  1196.     (f90-end-of-subprogram)
  1197.     (push-mark (point) t)
  1198.     (goto-char pos)
  1199.     (setq program (f90-beginning-of-subprogram))
  1200.     ;; The keywords in the preceding lists assume case-insensitivity.
  1201.     (if (string-match "XEmacs" emacs-version)
  1202.     (zmacs-activate-region)
  1203.       (setq mark-active t)
  1204.       (setq deactivate-mark nil))
  1205.     program))
  1206.  
  1207. (defun f90-comment-region (beg-region end-region)
  1208.   "Comment/uncomment every line in the region.
  1209. Insert f90-comment-region at the beginning of every line in the region
  1210. or, if already present, remove it."
  1211.   (interactive "*r")
  1212.   (let ((end (make-marker)))
  1213.     (set-marker end end-region)
  1214.     (goto-char beg-region)
  1215.     (beginning-of-line)
  1216.     (if (looking-at (regexp-quote f90-comment-region))
  1217.     (delete-region (point) (match-end 0))
  1218.       (insert f90-comment-region))
  1219.     (while (and  (zerop (forward-line 1))
  1220.          (< (point) (marker-position end)))
  1221.       (if (looking-at (regexp-quote f90-comment-region))
  1222.       (delete-region (point) (match-end 0))
  1223.     (insert f90-comment-region)))
  1224.     (set-marker end nil)))
  1225.  
  1226. (defun f90-indent-line (&optional no-update)
  1227.   "Indent current line as F90 code."
  1228.   (interactive)
  1229.   (let (indent (no-line-number nil) (pos (make-marker)) (case-fold-search t))
  1230.     (set-marker pos (point))
  1231.     (beginning-of-line)            ; Digits after & \n are not line-no
  1232.     (if (save-excursion (and (f90-previous-statement) (f90-line-continued)))
  1233.     (progn (setq no-line-number t) (skip-chars-forward " \t"))
  1234.       (f90-indent-line-no))
  1235.     (if (looking-at "!")
  1236.     (setq indent (f90-comment-indent))
  1237.       (if (and (looking-at "end") f90-smart-end)
  1238.         (f90-match-end))
  1239.       (setq indent (f90-calculate-indent)))
  1240.     (if (zerop (- indent (current-column)))
  1241.     nil
  1242.       (f90-indent-to indent no-line-number))
  1243.     ;; If initial point was within line's indentation,
  1244.     ;; position after the indentation.  Else stay at same point in text.
  1245.     (if (< (point) (marker-position pos))
  1246.     (goto-char (marker-position pos)))
  1247.     (if (not no-update) (f90-update-line))
  1248.     (if (and auto-fill-function
  1249.          (> (save-excursion (end-of-line) (current-column)) fill-column))
  1250.     (save-excursion (f90-do-auto-fill)))
  1251.     (set-marker pos nil)))
  1252.  
  1253. (defun f90-indent-new-line ()
  1254.   "Reindent the current F90 line, insert a newline and indent the newline.
  1255. An abbrev before point is expanded if `abbrev-mode' is non-nil.
  1256. If run in the middle of a line, the line is not broken."
  1257.   (interactive)
  1258.   (let (string cont (case-fold-search t))
  1259.     (if abbrev-mode (expand-abbrev))
  1260.     (beginning-of-line)            ; Reindent where likely to be needed.
  1261.     (f90-indent-line-no)
  1262.     (if (or (looking-at "\\(end\\|else\\|!\\)"))
  1263.     (f90-indent-line 'no-update))
  1264.     (end-of-line)
  1265.     (delete-horizontal-space)        ;Destroy trailing whitespace
  1266.     (setq string (f90-in-string))
  1267.     (setq cont (f90-line-continued))
  1268.     (if (and string (not cont)) (insert "&"))
  1269.     (f90-update-line)
  1270.     (newline)
  1271.     (if (or string (and cont f90-beginning-ampersand)) (insert "&"))
  1272.     (f90-indent-line 'no-update)))
  1273.  
  1274.  
  1275. (defun f90-indent-region (beg-region end-region)
  1276.   "Indent every line in region by forward parsing."
  1277.   (interactive "*r")
  1278.   (let ((end-region-mark (make-marker)) (save-point (point-marker))
  1279.     (block-list nil) ind-lev ind-curr ind-b cont
  1280.     struct beg-struct end-struct)
  1281.     (set-marker end-region-mark end-region)
  1282.     (goto-char beg-region)
  1283.     ;; first find a line which is not a continuation line or comment
  1284.     (beginning-of-line)
  1285.     (while (and (looking-at "[ \t]*[0-9]*\\(!\\|#\\|[ \t]*$\\)")
  1286.         (progn (f90-indent-line 'no-update)
  1287.                (zerop (forward-line 1)))
  1288.         (< (point) end-region-mark)))
  1289.     (setq cont (f90-present-statement-cont))
  1290.     (while (and (or (eq cont 'middle) (eq cont 'end))
  1291.         (f90-previous-statement))
  1292.       (setq cont (f90-present-statement-cont)))
  1293.     ;; process present line for beginning of block
  1294.     (setq f90-cache-position (point))
  1295.     (f90-indent-line 'no-update)
  1296.     (setq ind-lev (f90-current-indentation))
  1297.     (setq ind-curr ind-lev)
  1298.     (beginning-of-line) (skip-chars-forward " \t0-9")
  1299.     (setq struct nil)
  1300.     (setq ind-b (cond ((setq struct (f90-looking-at-do)) f90-do-indent)
  1301.               ((or (setq struct (f90-looking-at-if-then))
  1302.                (setq struct (f90-looking-at-select-case))
  1303.                (setq struct (f90-looking-at-where-or-forall))
  1304.                (looking-at f90-else-like-re))
  1305.                f90-if-indent)
  1306.               ((setq struct (f90-looking-at-type-like))
  1307.                f90-type-indent)
  1308.               ((or(setq struct (f90-looking-at-program-block-start))
  1309.               (looking-at "contains[ \t]*\\($\\|!\\)"))
  1310.                f90-program-indent)))
  1311.     (if ind-b (setq ind-lev (+ ind-lev ind-b)))
  1312.     (if struct (setq block-list (cons struct block-list)))
  1313.     (while (and (f90-line-continued) (zerop (forward-line 1))
  1314.         (< (point) end-region-mark))
  1315.       (if (not (zerop (- (current-indentation) 
  1316.              (+ ind-curr f90-continuation-indent))))
  1317.       (f90-indent-to (+ ind-curr f90-continuation-indent) 'no-line-no)))
  1318.     ;; process all following lines
  1319.     (while (and  (zerop (forward-line 1)) (< (point) end-region-mark))
  1320.       (beginning-of-line)
  1321.       (f90-indent-line-no)
  1322.       (setq f90-cache-position (point))
  1323.       (cond ((looking-at "[ \t]*$") (setq ind-curr 0))
  1324.         ((looking-at "[ \t]*#") (setq ind-curr 0))
  1325.         ((looking-at "!") (setq ind-curr (f90-comment-indent)))
  1326.         ((f90-no-block-limit) (setq ind-curr ind-lev))
  1327.         ((looking-at f90-else-like-re) (setq ind-curr
  1328.                          (- ind-lev f90-if-indent)))
  1329.         ((looking-at "contains[ \t]*\\($\\|!\\)")
  1330.          (setq ind-curr (- ind-lev f90-program-indent)))
  1331.         ((setq ind-b
  1332.            (cond ((setq struct (f90-looking-at-do)) f90-do-indent)
  1333.              ((or (setq struct (f90-looking-at-if-then))
  1334.                   (setq struct (f90-looking-at-select-case))
  1335.                   (setq struct (f90-looking-at-where-or-forall)))
  1336.               f90-if-indent)
  1337.              ((setq struct (f90-looking-at-type-like))
  1338.               f90-type-indent)
  1339.              ((setq struct (f90-looking-at-program-block-start))
  1340.               f90-program-indent)))
  1341.          (setq ind-curr ind-lev)
  1342.          (if ind-b (setq ind-lev (+ ind-lev ind-b)))
  1343.          (setq block-list (cons struct block-list)))
  1344.         ((setq end-struct (f90-looking-at-program-block-end))
  1345.          (setq beg-struct (car block-list)
  1346.            block-list (cdr block-list))
  1347.          (if f90-smart-end 
  1348.          (save-excursion
  1349.            (f90-block-match (car beg-struct)(car (cdr beg-struct))
  1350.                     (car end-struct)(car (cdr end-struct)))))
  1351.          (setq ind-b
  1352.            (cond ((looking-at f90-end-if-re) f90-if-indent)
  1353.              ((looking-at "end[ \t]*do\\>")  f90-do-indent)
  1354.              ((looking-at f90-end-type-re) f90-type-indent)
  1355.              ((f90-looking-at-program-block-end)
  1356.               f90-program-indent)))
  1357.          (if ind-b (setq ind-lev (- ind-lev ind-b)))
  1358.          (setq ind-curr ind-lev))
  1359.         (t (setq ind-curr ind-lev)))
  1360.       ;; do the indentation if necessary
  1361.       (if (not (zerop (- ind-curr (current-column))))
  1362.       (f90-indent-to ind-curr))
  1363.       (while (and (f90-line-continued) (zerop (forward-line 1))
  1364.           (< (point) end-region-mark))
  1365.     (if (not (zerop (- (current-indentation) 
  1366.                (+ ind-curr f90-continuation-indent))))
  1367.         (f90-indent-to (+ ind-curr f90-continuation-indent) 'no-line-no))))
  1368.     ;; restore point etc
  1369.     (setq f90-cache-position nil)
  1370.     (goto-char save-point)
  1371.     (set-marker end-region-mark nil)
  1372.     (set-marker save-point nil)
  1373.     (if (string-match "XEmacs" emacs-version)
  1374.     (zmacs-deactivate-region)
  1375.       (deactivate-mark))))
  1376.  
  1377. (defun f90-indent-subprogram ()
  1378.   "Properly indent the subprogram which contains point."
  1379.   (interactive)
  1380.   (save-excursion
  1381.     (let (program)
  1382.       (setq program (f90-mark-subprogram))
  1383.       (if program
  1384.       (progn
  1385.         (message "Indenting %s %s..."
  1386.              (car program) (car (cdr program)))
  1387.         (f90-indent-region (point) (mark))
  1388.         (message "Indenting %s %s...done"
  1389.              (car program) (car (cdr program))))
  1390.     (message "Indenting the whole file...")
  1391.     (f90-indent-region (point) (mark))
  1392.     (message "Indenting the whole file...done")))))
  1393.  
  1394. ;; autofill and break-line
  1395. (defun f90-break-line (&optional no-update)
  1396.   "Break line at point, insert continuation marker(s) and indent."
  1397.   (interactive)
  1398.   (let (ctype)
  1399.     (cond ((f90-in-string)
  1400.        (insert "&") (newline) (insert "&"))
  1401.       ((f90-in-comment)
  1402.        (delete-horizontal-space)
  1403.        (setq ctype (f90-get-present-comment-type))
  1404.        (newline) (insert (concat ctype " ")))
  1405.       (t (delete-horizontal-space)
  1406.          (insert "&")
  1407.          (if (not no-update) (f90-update-line))
  1408.          (newline)
  1409.          (if f90-beginning-ampersand (insert "& ")))))
  1410.   (if (not no-update) (f90-indent-line)))
  1411.   
  1412. (defun f90-find-breakpoint ()
  1413.   "From fill-column, search backward for break-delimiter."
  1414.   (let ((bol (f90-get-beg-of-line)))
  1415.     (re-search-backward f90-break-delimiters bol)
  1416.     (if f90-break-before-delimiters
  1417.     (progn (backward-char)
  1418.            (if (not (looking-at f90-no-break-re))
  1419.            (forward-char)))
  1420.       (if (looking-at f90-no-break-re)
  1421.       (forward-char 2)
  1422.     (forward-char)))))
  1423.  
  1424. (defun f90-auto-fill-mode (arg)
  1425.   "Toggle f90-auto-fill mode.
  1426. With ARG, turn `f90-auto-fill' mode on iff ARG is positive.
  1427. In `f90-auto-fill' mode, inserting a space at a column beyond `fill-column'
  1428. automatically breaks the line at a previous space."
  1429.   (interactive "P")
  1430.   (prog1 (setq auto-fill-function
  1431.            (if (if (null arg)
  1432.                (not auto-fill-function)
  1433.              (> (prefix-numeric-value arg) 0))
  1434.            'f90-do-auto-fill))
  1435.     (force-mode-line-update)))
  1436.  
  1437. (defun f90-do-auto-fill ()
  1438.   "Break line if non-white characters beyond fill-column."
  1439.   (interactive)
  1440.   ;; Break the line before or after the last delimiter (non-word char).
  1441.   ;; Will not break **, //, or => (specified by f90-no-break-re).
  1442.   ;; Start by checking that line is longer than fill-column.
  1443.   (if (> (save-excursion (end-of-line) (current-column)) fill-column)
  1444.       (progn
  1445.     (move-to-column fill-column)
  1446.     (if (and (looking-at "[ \t]*$") (not (f90-in-string)))
  1447.         (delete-horizontal-space)
  1448.       (f90-find-breakpoint)
  1449.       (f90-break-line)
  1450.       (end-of-line)))))
  1451.  
  1452. (defun f90-join-lines ()
  1453.   "Join present line with next line, if this line ends with \&."
  1454.   (interactive)
  1455.   (let (pos (oldpos (point)))
  1456.     (end-of-line)
  1457.     (skip-chars-backward " \t")
  1458.     (cond ((= (preceding-char) ?&)
  1459.        (delete-char -1)
  1460.        (setq pos (point))
  1461.        (forward-line 1)
  1462.        (skip-chars-forward " \t")
  1463.        (if (looking-at "\&") (delete-char 1))
  1464.        (delete-region pos (point))
  1465.        (if (not (f90-in-string))
  1466.            (progn (delete-horizontal-space) (insert " ")))
  1467.        (if (and auto-fill-function
  1468.             (> (save-excursion (end-of-line)
  1469.                        (current-column))
  1470.                fill-column))
  1471.            (f90-do-auto-fill))
  1472.        (goto-char oldpos)
  1473.        t))))
  1474.  
  1475. (defun f90-fill-region (beg-region end-region)
  1476.   "Fill every line in region by forward parsing. Join lines if possible."
  1477.   (interactive "*r")
  1478.   (let ((end-region-mark (make-marker))
  1479.     (f90-smart-end nil) (f90-auto-keyword-case nil) indent (go-on t)
  1480.     (af-function auto-fill-function) (auto-fill-function nil))
  1481.     (set-marker end-region-mark end-region)
  1482.     (goto-char beg-region)
  1483.     (while go-on
  1484.       ;; join as much as possible
  1485.       (while (f90-join-lines));
  1486.       (setq indent (+ (f90-current-indentation) f90-continuation-indent))
  1487.       ;; chop the line if necessary
  1488.       (while (> (save-excursion (end-of-line) (current-column))
  1489.         fill-column)
  1490.     (move-to-column fill-column)
  1491.     (if (and (looking-at "[ \t]*$") (not (f90-in-string)))
  1492.         (delete-horizontal-space)
  1493.       (f90-find-breakpoint)
  1494.       (f90-break-line 'no-update)
  1495.       (f90-indent-to indent 'no-line-no)))
  1496.       (setq go-on (and  (< (point) (marker-position end-region-mark))
  1497.             (zerop (forward-line 1))))
  1498.       (setq f90-cache-position (point)))
  1499.     (setq auto-fill-function af-function)
  1500.     (setq f90-cache-position nil)
  1501.     (if (string-match "XEmacs" emacs-version)
  1502.     (zmacs-deactivate-region)
  1503.       (deactivate-mark))))
  1504.  
  1505. (defun f90-block-match (beg-block beg-name end-block end-name)
  1506.   "Match end-struct with beg-struct and complete end-block if possible.
  1507. Leave point at the end of line."
  1508.   (search-forward "end" (f90-get-end-of-line))
  1509.   (catch 'no-match
  1510.     (if (not (f90-equal-symbols beg-block end-block))
  1511.     (if end-block
  1512.         (progn
  1513.           (message "END %s does not match %s." end-block beg-block)
  1514.           (end-of-line) 
  1515.           (throw 'no-match nil))
  1516.       (message "Inserting %s." beg-block)
  1517.       (insert (concat " " beg-block)))
  1518.       (search-forward end-block))
  1519.     (if (not (f90-equal-symbols beg-name end-name))
  1520.     (cond ((and beg-name (not end-name)) 
  1521.            (message "Inserting %s." beg-name)
  1522.            (insert (concat " " beg-name)))
  1523.           ((and beg-name end-name) 
  1524.            (message "Replacing %s with %s." end-name beg-name)
  1525.            (search-forward end-name)
  1526.            (replace-match beg-name))
  1527.           ((and (not beg-name) end-name) 
  1528.            (message "Deleting %s." end-name)
  1529.            (search-forward end-name)
  1530.            (replace-match "")))
  1531.       (if end-name (search-forward end-name)))
  1532.     (if (not (looking-at "[ \t]*!")) (delete-horizontal-space))))
  1533.  
  1534. (defun f90-match-end ()
  1535.   "From an end foo statement, find the corresponding foo including name."
  1536.   (interactive)
  1537.   (let ((count 1) (top-of-window (window-start)) (matching-beg nil)
  1538.     (end-point (point)) (case-fold-search t)
  1539.     beg-name end-name beg-block end-block end-struct)
  1540.     (if (save-excursion (beginning-of-line) (skip-chars-forward " \t0-9")
  1541.             (setq end-struct (f90-looking-at-program-block-end)))
  1542.     (progn
  1543.       (setq end-block (car end-struct))
  1544.       (setq end-name  (car (cdr end-struct)))
  1545.       (save-excursion
  1546.         (beginning-of-line)
  1547.         (while 
  1548.         (and (not (zerop count))
  1549.              (let ((stop nil) notexist)
  1550.                (while (not stop)
  1551.              (setq notexist
  1552.                    (not (re-search-backward 
  1553.                      (concat "\\(" f90-blocks-re "\\)") nil t)))
  1554.              (if notexist
  1555.                  (setq stop t)
  1556.                (setq stop
  1557.                  (not (or (f90-in-string)
  1558.                       (f90-in-comment))))))
  1559.                (not notexist)))
  1560.           (beginning-of-line) (skip-chars-forward " \t0-9")
  1561.           (cond ((setq matching-beg
  1562.                (cond
  1563.                 ((f90-looking-at-do))
  1564.                 ((f90-looking-at-if-then))
  1565.                 ((f90-looking-at-where-or-forall))
  1566.                 ((f90-looking-at-select-case))
  1567.                 ((f90-looking-at-type-like))
  1568.                 ((f90-looking-at-program-block-start))))
  1569.              (setq count (- count 1)))
  1570.             ((looking-at (concat "end[ \t]*" f90-blocks-re "\\b"))
  1571.              (setq count (+ count 1)))))
  1572.         (if (not (zerop count))
  1573.         (message "No matching beginning.")
  1574.           (f90-update-line)
  1575.           (if (eq f90-smart-end 'blink)
  1576.           (if (< (point) top-of-window)
  1577.               (message "Matches %d: %s"
  1578.                    (what-line)
  1579.                    (buffer-substring
  1580.                 (progn (beginning-of-line) (point))
  1581.                 (progn (end-of-line) (point))))
  1582.             (sit-for 1)))
  1583.           (setq beg-block (car matching-beg))
  1584.           (setq beg-name (car (cdr matching-beg)))
  1585.           (goto-char end-point)
  1586.           (beginning-of-line)
  1587.           (f90-block-match beg-block beg-name end-block end-name)))))))
  1588.  
  1589. (defun f90-insert-end ()
  1590.   "Inserts an complete end statement matching beginning of present block."
  1591.   (interactive)
  1592.   (let ((f90-smart-end (if f90-smart-end f90-smart-end 'blink)))
  1593.     (insert "end")
  1594.     (f90-indent-new-line)))
  1595.  
  1596. ;; abbrevs and keywords
  1597.  
  1598. (defun f90-abbrev-start ()
  1599.   "Typing `\\[help-command] or `? lists all the F90 abbrevs. 
  1600. Any other key combination is executed normally."
  1601.   (interactive)
  1602.   (let (e c)
  1603.     (insert last-command-char)
  1604.     (if (string-match "XEmacs" emacs-version)
  1605.     (progn
  1606.       (setq e (next-command-event))
  1607.       (setq c (event-to-character e)))
  1608.       (setq c (read-event)))
  1609.     ;; insert char if not equal to `?'
  1610.     (if (or (= c ??) (eq c help-char))
  1611.     (f90-abbrev-help)
  1612.       (if (string-match "XEmacs" emacs-version)
  1613.       (setq unread-command-event e)
  1614.     (setq unread-command-events (list c))))))
  1615.  
  1616. (defun f90-abbrev-help ()
  1617.   "List the currently defined abbrevs in F90 mode."
  1618.   (interactive)
  1619.   (message "Listing abbrev table...")
  1620.   (display-buffer (f90-prepare-abbrev-list-buffer))
  1621.   (message "Listing abbrev table...done"))
  1622.  
  1623. (defun f90-prepare-abbrev-list-buffer ()
  1624.   (save-excursion
  1625.     (set-buffer (get-buffer-create "*Abbrevs*"))
  1626.     (erase-buffer)
  1627.     (insert-abbrev-table-description 'f90-mode-abbrev-table t)
  1628.     (goto-char (point-min))
  1629.     (set-buffer-modified-p nil)
  1630.     (edit-abbrevs-mode))
  1631.   (get-buffer-create "*Abbrevs*"))
  1632.  
  1633. (defun f90-upcase-keywords ()
  1634.   "Upcase all F90 keywords in the buffer."
  1635.   (interactive)
  1636.   (f90-change-keywords 'upcase-word))
  1637.  
  1638. (defun f90-capitalize-keywords ()
  1639.   "Capitalize all F90 keywords in the buffer."
  1640.   (interactive)
  1641.   (f90-change-keywords 'capitalize-word))
  1642.  
  1643. (defun f90-downcase-keywords ()
  1644.   "Downcase all F90 keywords in the buffer."
  1645.   (interactive)
  1646.   (f90-change-keywords 'downcase-word))
  1647.  
  1648. (defun f90-upcase-region-keywords (beg end)
  1649.   "Upcase all F90 keywords in the region."
  1650.   (interactive "*r")
  1651.   (f90-change-keywords 'upcase-word beg end))
  1652.  
  1653. (defun f90-capitalize-region-keywords (beg end)
  1654.   "Capitalize all F90 keywords in the region."
  1655.   (interactive "*r")
  1656.   (f90-change-keywords 'capitalize-word beg end))
  1657.  
  1658. (defun f90-downcase-region-keywords (beg end)
  1659.   "Downcase all F90 keywords in the region."
  1660.   (interactive "*r")
  1661.   (f90-change-keywords 'downcase-word beg end))
  1662.  
  1663. ;; Change the keywords according to argument.
  1664. (defun f90-change-keywords (change-word &optional beg end)
  1665.   (save-excursion
  1666.     (setq beg (if beg beg (point-min)))
  1667.     (setq end (if end end (point-max)))
  1668.     (let ((keyword-re 
  1669.        (concat "\\("
  1670.            f90-keywords-re "\\|" f90-procedures-re "\\|"
  1671.            f90-hpf-keywords-re "\\|" f90-operators-re "\\)"))
  1672.       (ref-point (point-min)) state
  1673.       (modified (buffer-modified-p)) saveword back-point)
  1674.       (goto-char beg)
  1675.       (unwind-protect
  1676.       (while (re-search-forward keyword-re end t)
  1677.         (if (progn
  1678.           (setq state (parse-partial-sexp ref-point (point)))
  1679.           (or (nth 3 state) (nth 4 state)
  1680.               (save-excursion    ; Check for cpp directive.
  1681.             (beginning-of-line)
  1682.             (skip-chars-forward " \t0-9")
  1683.             (looking-at "#"))))
  1684.         ()
  1685.           (setq ref-point (point)
  1686.             back-point (save-excursion (backward-word 1) (point)))
  1687.           (setq saveword (buffer-substring back-point ref-point))
  1688.           (funcall change-word -1)
  1689.           (or (string= saveword (buffer-substring back-point ref-point))
  1690.           (setq modified t))))
  1691.     (or modified (set-buffer-modified-p nil))))))
  1692.  
  1693. (provide 'f90)
  1694.  
  1695. ;;; f90.el ends here
  1696.